import {config} from '../config'

const tips = {
    1: '抱歉,出现错误',
    1005: 'appKey无效',
    300: '不存在'
}


class HTTP {
    request(params) {

        wx.request(
            {
                url: config.apiBaseUrl + params.url,
                method: params.method || "GET",
                data: params.data,
                header: {
                    'contentType': 'application/json',
                    'appKey': config.appkey
                },

                success: (res) => {

                    let code = res.statusCode.toString()
                    if (code.startsWith('2')) {
                        params.success && params.success(res.data)

                    } else {
                        this._showError(res.data.errorCode)

                    }
                },
                fail: (err) => {
                    this._showError()

                }
            }
        )
    }


    _showError(errCode = 1) {

        const tip = tips[errCode]
        wx.showToast({
            title: tip ? tip : tips[errCode],
            icon: 'none',
            duration: 2000
        })

    }


}


export {HTTP}
